package com.snpic.appaw.module.infra.api.config;

import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.module.infra.dal.dataobject.config.ConfigDO;
import com.snpic.appaw.module.infra.enums.InfraConstant;
import com.snpic.appaw.module.infra.service.config.ConfigService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import static com.snpic.appaw.framework.common.pojo.CommonResult.success;
import static com.snpic.appaw.module.infra.enums.InfraConstant.SN_CONFIG_KEY;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class ConfigApiImpl implements ConfigApi {

	@Resource
	private ConfigService configService;

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public CommonResult<String> getConfigValueByKey(String key) {
		ConfigDO config = configService.getConfigByKey(key);
		return success(config != null ? config.getValue() : null);
	}

	/**
	 * 申能C端详专用配置参数值查询接口
	 * @param name
	 * @return
	 */
	@Override
	public String getConfigValue(String name) {
		Object value = redisTemplate.opsForHash().get(SN_CONFIG_KEY, name);
		return value != null ? ((String) value).split("\\|")[0] : null;
	}

}
